cmake_minimum_required(VERSION 3.10)

if(NOT DEFINED ENV{DM2016_RT})
	MESSAGE(FATAL_ERROR "请设置环境变量DM2016_RT")
endif()

if(WIN32)
	set(WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
	add_definitions(-DJSON_DLL_BUILD)
endif()

set(EXECUTABLE_OUTPUT_PATH $ENV{DM2016_RT}/bin)

if( UNIX )
	set(LIBRARY_OUTPUT_PATH $ENV{DM2016_RT}/lib)
else()
	set(LIBRARY_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH})
	set(ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH})
endif()

#set(Boost_USE_STATIC_LIBS OFF)
#set(Boost_USE_DEBUG_LIBS OFF)

include_directories($ENV{DM2016_DEV}/include)

if( DEFINED ENV{ARM} )
	ADD_DEFINITIONS(-D_ARM_ )
	SET(CMAKE_SYSTEM_NAME Linux)
	SET(CMAKE_C_COMPILER $ENV{CC})
	SET(CMAKE_CXX_COMPILER $ENV{CXX})
endif()

if( DEFINED ENV{CXX_11})
	set(CMAKE_CXX_STANDARD 11)
endif()

if(DEFINED ENV{CXXTEST_DIR})
	MESSAGE(STATUS "增加CXXTEST目录$ENV{CXXTEST_DIR}")
	if(DEFINED CMAKE_PREFIX_PATH)
		list(APPEND CMAKE_PREFIX_PATH $ENV{CXXTEST_DIR})
	else()
		set(CMAKE_PREFIX_PATH $ENV{CXXTEST_DIR})
	endif()
endif()

if(DEFINED ENV{BOOST_DIR})
	MESSAGE(STATUS "增加BOOST目录$ENV{BOOST_DIR}")
	set(BOOST_ROOT $ENV{BOOST_DIR})
endif()

if(WIN32)
	add_compile_options(/wd4244)
	add_compile_options(/wd4251)
	add_compile_options(/wd4275)
	add_compile_options(/wd4996)
endif()

macro(DB_SELECT ARG_TARGETGET)
	if( $ENV{DB_TYPE} MATCHES "mysql" )
		ADD_DEFINITIONS(-DDB_MYSQL )
	elseif( $ENV{DB_TYPE} MATCHES "postgresql" )	
		ADD_DEFINITIONS(-DDB_POSTGRESQL)	
	else()
		ADD_DEFINITIONS(-DDB_SQLITE)
	endif()

	if(NOT DEFINED ENV{NO_MYSQL})
		target_link_libraries(${ARG_TARGETGET} mysqlcppconn mysqlclient)
	else()
		ADD_DEFINITIONS(-DNO_MYSQL)
	endif()

	if(NOT DEFINED ENV{NO_POSTGRESQL})		
		target_link_libraries(${ARG_TARGETGET} pq)
	else()
		ADD_DEFINITIONS(-DNO_POSTGRESQL)
	endif()

	if( DEFINED ENV{SQLITE_DIR} )
		include_directories($ENV{SQLITE_DIR}/include)
		target_link_directories(${ARG_TARGETGET} PUBLIC $ENV{SQLITE_DIR}/lib)
		target_link_libraries(${ARG_TARGETGET} sqlite3)
	else()
		if( DEFINED ENV{SQLITE_CMAKE} )			
			find_package(SQLite3)
			if(SQLite3_FOUND)
				include_directories(${SQLite3_INCLUDE_DIRS})
				target_link_libraries(${ARG_TARGETGET} ${SQLite3_LIBRARY})
			else()
				MESSAGE(FATAL_ERROR "Can not find package sqlite3")
			endif()
		else()
			target_link_libraries(${ARG_TARGETGET} sqlite3)
		endif()
	endif()
endmacro (DB_SELECT)